package cn.yunhe.action;

import cn.yunhe.entity.Admin;
import cn.yunhe.service.IAdminService;
import cn.yunhe.utils.BaseAction;

import com.opensymphony.xwork2.ModelDriven;
import lombok.Getter;
import lombok.Setter;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;


import java.util.List;

@Controller
@Getter
@Setter
@ParentPackage(value = "struts-default")
public class AdminAction extends BaseAction implements ModelDriven {

    private Admin admin = new Admin();

    @Autowired
    @Qualifier(value = "adminService")
    private IAdminService adminService;

    @Action(value = "*adminList", results = {
            @Result(location = "/succ.jsp")
    })

    public String getAll() {
        List<Admin> list = adminService.getAll();
        getMapRequest().put("list", list);
        return SUCCESS;
    }

    @Action(value = "adminReg", results = {
            @Result(location = "/succ.jsp"),
            @Result(name = INPUT, location = "/reg.jsp")
    })
    public String register() {
        admin.getA_name();
        admin.getA_pwd();
        if (admin != null) {
            adminService.register(admin);
            return SUCCESS;
        } else {
            return INPUT;
        }
    }

    @Action(value = "adminLog", results = {
            @Result(
                    location = "adminList",type = "redirect"),
            @Result(name = INPUT, location = "/log.jsp")
    })
    public String login() {
        Admin admin1 = adminService.login(admin.getA_name(), admin.getA_pwd());
        if (admin1 != null) {
            getSession().setAttribute("admin", admin1);
            return SUCCESS;
        } else {
            return INPUT;
        }

    }

    @Override
    public Object getModel() {
        return admin;
    }
}
